package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PetCategory;
import com.ruoyi.system.service.IPetCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类信息Controller
 * 
 * @author 李姝函
 * @date 2024-09-11
 */
@Controller
@RequestMapping("/system/category")
public class PetCategoryController extends BaseController
{
    private String prefix = "system/category";

    @Autowired
    private IPetCategoryService petCategoryService;

    @RequiresPermissions("system:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询分类信息列表
     */
    @RequiresPermissions("system:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PetCategory petCategory)
    {
        startPage();
        List<PetCategory> list = petCategoryService.selectPetCategoryList(petCategory);
        return getDataTable(list);
    }

    /**
     * 导出分类信息列表
     */
    @RequiresPermissions("system:category:export")
    @Log(title = "分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PetCategory petCategory)
    {
        List<PetCategory> list = petCategoryService.selectPetCategoryList(petCategory);
        ExcelUtil<PetCategory> util = new ExcelUtil<PetCategory>(PetCategory.class);
        return util.exportExcel(list, "分类信息数据");
    }

    /**
     * 新增分类信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存分类信息
     */
    @RequiresPermissions("system:category:add")
    @Log(title = "分类信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PetCategory petCategory)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        petCategory.setCreateBy(sysUser.getUserName());
        petCategory.setUpdateBy(sysUser.getUserName());
        return toAjax(petCategoryService.insertPetCategory(petCategory));
    }

    /**
     * 修改分类信息
     */
    @RequiresPermissions("system:category:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PetCategory petCategory = petCategoryService.selectPetCategoryById(id);
        mmap.put("petCategory", petCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存分类信息
     */
    @RequiresPermissions("system:category:edit")
    @Log(title = "分类信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PetCategory petCategory)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        petCategory.setUpdateBy(sysUser.getUserName());
        return toAjax(petCategoryService.updatePetCategory(petCategory));
    }

    /**
     * 删除分类信息
     */
    @RequiresPermissions("system:category:remove")
    @Log(title = "分类信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(petCategoryService.deletePetCategoryByIds(ids));
    }
}
